package com.umpay.service.consum.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.umpay.dao.online.TransDao;
import com.umpay.dao.online.consum.ConsumOrderOnlineDao;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.entity.ConsumeBalControl;
import com.umpay.entity.Trans;
import com.umpay.entity.consum.ConsumOrder;
import com.umpay.exception.BusinessException;
import com.umpay.service.consum.ConsumeBalanceService;

@Service("ConsumeBalanceService")
public class ConsumeBalanceServiceImpl implements ConsumeBalanceService {
	
	private static final Logger log = LogManager.getLogger(ConsumeBalanceServiceImpl.class);

	
	@Override
	public ConsumOrder find(ConsumOrder order) {		
//		log.info("【查询消费订单表信息】"+order);
////		ConsumOrder consumOrder = consumOrderOnlineDao.find(order);
//		log.info("【查询消费订单表信息】"+consumOrder);
		return null;
	}

	@Override
	public int update(ConsumOrder order) {
		int result = -1;
		log.info("【更新消费订单表信息开始】");
		try {
			log.info(String.format("更新消费订单状态及其他信息,:[%s]",order));
//			result = consumOrderOnlineDao.update(order);
			if(result!=1){
				log.info(String.format("更新订单表失败，更新的条数result：%s",result));
				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费订单表异常");
			}
		} catch (DataAccessException e) {
			log.error("【更新在线消费订单表】更新T_CONSUME_ORDER_0(0-5)失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费订单表失败");
		} 
		log.info("【更新消费订单表信息结束】");
		return result;
	}

	@Override
	@Transactional
	public void insert(ConsumOrder order) throws BusinessException{
		log.info(String.format("准备插入余额消费订单表数据：%s", order.toString()));
		try{
//			consumOrderOnlineDao.insert(order);
		}catch (DataAccessException e) {
			log.error("插入余额消费订单表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("插入余额消费订单表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
		log.info(String.format("消费订单表交易表插入成功！"));
	}

	@Override
	@Transactional
	public int updateStateAndInsertBalControl(ConsumOrder order, ConsumeBalControl consumeBalControl,Trans trans) {
		log.info(String.format("准备更新消费订单表数据：%s", order.toString()));
		log.info(String.format("准备插入余额控制表数据：%s", consumeBalControl.toString()));
		log.info(String.format("准备更新消费交易表数据：%s", trans.toString()));
		int result = -1;
		try {
//			result = consumOrderOnlineDao.update(order);
//			if(result!=1){
//				log.info(String.format("更新订单表失败，更新的条数result：%s",result));
//				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费订单表异常");
//			}
//			consumeBalControlOnline.insert(consumeBalControl);
//			result = consumTransOnlineDao.update(trans);
			if(result!=1){
				log.info(String.format("更新交易表失败，更新的条数result：%s",result));
				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费交易表异常");
			}
			
		}catch (DataAccessException e) {
			log.error("消费确认操作表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("消费确认操作表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
		return result;
	}

	@Override
	@Transactional
	public int updateConsumOrderAndTrans(ConsumOrder order, Trans consumeTrans) {
		int result = -1;
		log.info("【更新余额消费订单和交易表信息开始】");
//		try {
//			log.info(String.format("更新余额消费订单状态及其他信息,:[%s]",order));
//			log.info(String.format("更新余额消费交易状态及其他信息,:[%s]",consumeTrans));
//			result = consumOrderOnlineDao.update(order);
//			if(result!=1){
//				log.info(String.format("更新订单表失败，更新的条数result：%s",result));
//				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费订单表异常");
//			}
//			result=consumTransOnlineDao.update(consumeTrans);
//			if(result!=1){
//				log.info(String.format("更新交易表失败，更新的条数result：%s",result));
//				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费交易表异常");
//			}
//		} catch (DataAccessException e) {
//			log.error("【更新在线余额消费订单表】更新Order失败",e);
//			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线余额消费订单表失败");
//		} catch(Exception e){
//			log.error("【更新在线余额消费交易表】更新Trans失败",e);
//			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线余额消费交易表失败");
//		}
		log.info("【更新消费信息结束】");
		return result;
	}

	@Override
	@Transactional
	public int insertTransAndLockOrder(ConsumOrder order, Trans consumeTrans) {
		log.info(String.format("准备插入余额消费交易表数据：%s", consumeTrans.toString()));
		log.info(String.format("准备更新余额消费订单表数据：%s", order.toString()));
		int result = -1;
		try{
//			consumTransOnlineDao.insert(consumeTrans);
//			result = consumOrderOnlineDao.update(order);
			if(result!=1){
				log.info(String.format("更新订单表失败，更新的条数result：%s",result));
				throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线消费订单表异常");
			}
		}catch (DataAccessException e) {
            if (e instanceof DuplicateKeyException) {
                log.error(String.format("【交易表】插入主键冲突,插入交易信息[%s]",consumeTrans));
                throw new BusinessException(MerBusiEC.DB_DUPLICATE_KEY,"商户支付流水重复");
            }
			log.error("插入余额消费交易表和更新订单表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("插入余额消费交易表和更新订单表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
		log.info(String.format("插入余额消费交易表和更新订单表成功！"));
		return result;
	}

}
